/* wx的调用接口公用方法*/
import { success, SUCCESS } from './success';
import { start, START } from './start';
import { fail, FAIL } from './fail';
import checkStatus from './checkStatus';
import parseJSON from './parseJSON';
export default function appFetch(action, url, params, extra = {}, callback) {
    return (dispatch) => {
        if (!action) return;
        dispatch(start(action, { params, fetchResult: {}, ...extra }));
        callback && callback(START);

        fetch(url, {
            method: 'POST',
            body: JSON.stringify(params),
        }).then(checkStatus).then(parseJSON).then((fetchResult) => {
            if (fetchResult && fetchResult.code > 0) {
                dispatch(success(action, { params, fetchResult, ...extra }));
                callback && callback(SUCCESS, fetchResult);
            } else {
                dispatch(fail(action, { params, fetchResult: {}, ...extra }));
                callback && callback(FAIL, fetchResult);
            }
        });
    };
}
